package com.ulife.order.service.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * json 工具类
 * 
 * @author chendaoliang
 * 
 */
public final class JsonUtils {

	private JsonUtils(){
		
	}
	
	/**
	 * 将obj转为json串
	 * 
	 * @param obj
	 * @return
	 */
	public static String obj2Json(Object obj) {
		ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
		try {
			String jsonStr = objectMapper.writeValueAsString(obj);
			return jsonStr;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将json转为obj
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T json2Obj(String json, Class<T> clazz) {
		ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
		try {
			T obj = objectMapper.readValue(json, clazz);
			return obj;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	}